package com.nebula.util;

import java.util.Map;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;

public class PageUtil {
	public static Map<String, Object> pageInfo(PageInfo<?> pageInfo){
		Logger logger = LoggerFactory.getLogger(PageUtil.class); 
		Map<String, Object> map = Maps.newHashMap();
		int total = (int)pageInfo.getTotal();
		int pageNum = pageInfo.getPageNum();
		
		//总页数
		int totalPage = pageInfo.getPages();
				
		Vector<Integer> pageArr = new Vector<Integer>();
		int start = 1;
		int end = totalPage;
		if(totalPage > 10){
			if(pageNum <= 5){
				start = 1;
				end = 10;
			}
			else if(pageNum >= totalPage - 4){
				start = totalPage - 9;
				end = totalPage;
			}else {
				start = pageNum - 5;
				end = pageNum + 4;
			}
		}
		for(int i = start;i <= end;i++){
			pageArr.add(new Integer(i));
		}
		
		map.put("pageArr",pageArr);
		map.put("total", total);
		map.put("totalPages", totalPage);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageInfo.getPageSize());
		map.put("start", pageInfo.getStartRow());
		map.put("end", pageInfo.getEndRow());
		
		logger.info("pageArr:{},total:{},totalPages:{},pageNum:{},pageSize:{},start:{},end:{}"
				,pageArr,total,totalPage,pageNum,pageInfo.getPageSize(),pageInfo.getStartRow(),pageInfo.getEndRow());
		
		return map;
	}
}
